//
//  VSDrawerController.m
//  ViewSeer
//
//  Created by LevinYan on 2017/3/21.
//  Copyright © 2017年 LevinYan. All rights reserved.
//

#import "VSDrawerController.h"
#import "VSRevealController.h"
#import "VSTreeViewController.h"
#import "VSInspectorViewController.h"
#import "VSNode.h"



@interface VSDrawerController ()<RevealControllerDelegate,VSTreeViewControllerDelegate>

@property (nonatomic, readonly) VSTreeViewController *treeViewController;
@property (nonatomic, readonly) VSRevealController *revealController;
@property (nonatomic, readonly) VSInspectorViewController *inspectorViewController;

@end

@implementation VSDrawerController

+ (instancetype)controller
{
    VSTreeViewController *treeViewController = [VSTreeViewController new];
    VSRevealController *revealController = [VSRevealController new];
    VSInspectorViewController *inspectorViewController = [VSInspectorViewController new];
    VSDrawerController *drawerController = [[self alloc] initWithCenterViewController:revealController leftDrawerViewController:treeViewController rightDrawerViewController:inspectorViewController];
   
    [drawerController myInit];
    
    return drawerController;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.openDrawerGestureModeMask = MMOpenDrawerGestureModeBezelPanningCenterView;
    self.closeDrawerGestureModeMask = MMCloseDrawerGestureModeBezelPanningCenterView|
    MMCloseDrawerGestureModeTapCenterView;
    
    
}

- (void)myInit
{
    self.revealController.delegate = self;
    self.treeViewController.delegate = self;
}
- (void)viewWillAppear:(BOOL)animated
{
    
    UIView *rootView = [UIApplication sharedApplication].keyWindow.subviews.firstObject.subviews.firstObject.subviews.firstObject;
    VSNode *node = [VSNode node:rootView];
    self.treeViewController.rootNode = node;
    self.revealController.rootNode = node;

}
- (void)viewDidAppear:(BOOL)animated
{
}
- (VSTreeViewController*)treeViewController
{
    return (VSTreeViewController*)self.leftDrawerViewController;
}

- (VSRevealController*)revealController
{
    return (VSRevealController*)self.centerViewController;
}
- (VSInspectorViewController*)inspectorViewController
{
    return (VSInspectorViewController*)self.rightDrawerViewController;
}

#pragma mark - RevealControllerDelegate

- (void)revealControllerLeftTopButtonClicked
{
    [self openDrawerSide:MMDrawerSideLeft animated:YES completion:nil];

}
- (void)revealControllerrightTopButtonClicked
{
    [self openDrawerSide:MMDrawerSideRight animated:YES completion:nil];

}

- (void)revealControllerDidSelectedNode:(VSNode *)node
{
    self.title = node.name;
    [self.treeViewController setSelectedNode:node];
    
}
#pragma mark - VSTreeViewControllerDelegate

- (void)treeViewControllerDidSelectNode:(VSNode*)node
{
    [self.revealController setSelectedNode:node];
}

@end
